<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,200分)- 代表团坐车（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>某组织举行会议&#xff0c;来了多个代表团同时到达&#xff0c;接待处只有一辆汽车&#xff0c;可以同时接待多个代表团&#xff0c;为了提高车辆利用率&#xff0c;请帮接待员计算可以坐满车的接待方案&#xff0c;输出方案数量。</p> 
<p><br /> 约束:</p> 
<ol><li>一个团只能上一辆车&#xff0c;并且代表团人数 (代表团数量小于30&#xff0c;每个代表团人数小于30)小于汽车容量(汽车容量小于100)</li><li>需要将车辆坐满</li></ol> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<p>第一行 代表团人数&#xff0c;英文逗号隔开&#xff0c;代表团数量小于30&#xff0c;每个代表团人数小于30<br /> 第二行 汽车载客量&#xff0c;汽车容量小于100</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<p>坐满汽车的方案数量<br /> 如果无解输出0</p> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:500px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:412px;">5,4,2,3,2,4,9<br /> 10</td></tr><tr><td style="width:86px;">输出</td><td style="width:412px;">4</td></tr><tr><td style="width:86px;">说明</td><td style="width:412px;">解释 以下几种方式都可以坐满车&#xff0c;所以&#xff0c;优先接待输出为4<br /> [2,3,5]<br /> [2,4,4]<br /> [2,3,5]<br /> [2,4,4]</td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题可以转化为01背包的装满背包的方案数问题。</p> 
<p>解析可以参考&#xff1a;<a href="https://blog.csdn.net/qfc_128220/article/details/130318724?spm&#61;1001.2014.3001.5501" title="LeetCode - 494 目标和_伏城之外的博客-CSDN博客">LeetCode - 494 目标和_伏城之外的博客-CSDN博客</a></p> 
<p>也可以尝试下&#xff1a;<a href="https://fcqian.blog.csdn.net/article/details/129300635" rel="nofollow" title="华为校招机试 - 求和&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客">华为校招机试 - 求和&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客</a></p> 
<p></p> 
<hr /> 
<p>2023.10.14 </p> 
<p>根据考友反馈&#xff0c;本题存在异常格式输入&#xff0c;</p> 
<p></p> 
<p>我猜测是类似于</p> 
<p><a href="https://blog.csdn.net/qfc_128220/article/details/130770597" title="华为OD机试 - 生日礼物&#xff08;Java &amp; JS &amp; Python &amp; C&#xff09;_java生日礼物-CSDN博客">华为OD机试 - 生日礼物&#xff08;Java &amp; JS &amp; Python &amp; C&#xff09;_java生日礼物-CSDN博客</a></p> 
<p>中的格式异常输入&#xff0c;即可能输入如下&#xff1a;</p> 
<blockquote> 
 <p><span style="color:#fe2c24;">[</span>5,4,2,3,2,4,9<span style="color:#fe2c24;">]</span><br /> 10</p> 
</blockquote> 
<p></p> 
<p>或者是类似于</p> 
<p><a href="https://blog.csdn.net/qfc_128220/article/details/127418403" title="华为OD机试 - 数组拼接&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客">华为OD机试 - 数组拼接&#xff08;Java &amp; JS &amp; Python&#xff09;_伏城之外的博客-CSDN博客</a></p> 
<p>中的格式异常输入&#xff0c;即可能输入如下&#xff1a;</p> 
<blockquote> 
 <p>5,4,2,3,2<span style="color:#fe2c24;">,,</span>4,9<br /> 10</p> 
</blockquote> 
<p></p> 
<p>具体是哪种输入格式&#xff0c;抽到该题的考友没有反馈&#xff0c;但是可以在考试时&#xff0c;通过提示的异常日志来看。</p> 
<p>上面两种格式的异常处理&#xff0c;可以参照对应博客内的处理。</p> 
<p></p> 
<p>不处理输入格式异常也可得90%通过率。</p> 
<p> </p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Java算法源码</h4> 
<p>二维数组解法</p> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    Integer[] nums &#61;
        Arrays.stream(sc.nextLine().split(&#34;,&#34;)).map(Integer::parseInt).toArray(Integer[]::new);

    int bag &#61; Integer.parseInt(sc.nextLine());

    System.out.println(getResult(nums, bag));
  }

  private static int getResult(Integer[] nums, int bag) {
    int n &#61; nums.length;

    int[][] dp &#61; new int[n &#43; 1][bag &#43; 1];
    dp[0][0] &#61; 1;

    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
      int num &#61; nums[i - 1];
      for (int j &#61; 0; j &lt;&#61; bag; j&#43;&#43;) {
        if (j &lt; num) {
          dp[i][j] &#61; dp[i - 1][j];
        } else {
          dp[i][j] &#61; dp[i - 1][j] &#43; dp[i - 1][j - num];
        }
      }
    }

    return dp[n][bag];
  }
}
</code></pre> 
<p>滚动数组优化解法</p> 
<pre><code class="language-java">import java.util.Arrays;
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    Integer[] nums &#61;
        Arrays.stream(sc.nextLine().split(&#34;,&#34;)).map(Integer::parseInt).toArray(Integer[]::new);

    int bag &#61; Integer.parseInt(sc.nextLine());

    System.out.println(getResult(nums, bag));
  }

  private static int getResult(Integer[] nums, int bag) {
    int n &#61; nums.length;

    int[] dp &#61; new int[bag &#43; 1];
    dp[0] &#61; 1;

    for (int i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
      int num &#61; nums[i - 1];
      for (int j &#61; bag; j &gt;&#61; num; j--) {
        dp[j] &#61; dp[j] &#43; dp[j - num];
      }
    }

    return dp[bag];
  }
}
</code></pre> 
<p></p> 
<h4 id="JS%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JS算法源码</h4> 
<p>二维数组解法</p> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61; 2) {
    const nums &#61; lines[0].split(&#34;,&#34;).map(Number);
    const bag &#61; lines[1] - 0;
    console.log(getResult(nums, bag));
    lines.length &#61; 0;
  }
});

function getResult(nums, bag) {
  const n &#61; nums.length;

  const dp &#61; new Array(n &#43; 1).fill(0).map(() &#61;&gt; new Array(bag &#43; 1).fill(0));
  dp[0][0] &#61; 1;

  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
    const num &#61; nums[i - 1];
    for (let j &#61; 0; j &lt;&#61; bag; j&#43;&#43;) {
      if (j &lt; num) {
        dp[i][j] &#61; dp[i - 1][j];
      } else {
        dp[i][j] &#61; dp[i - 1][j] &#43; dp[i - 1][j - num];
      }
    }
  }

  return dp[n][bag];
}
</code></pre> 
<p>滚动数组优化解法</p> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61; 2) {
    const nums &#61; lines[0].split(&#34;,&#34;).map(Number);
    const bag &#61; lines[1] - 0;
    console.log(getResult(nums, bag));
    lines.length &#61; 0;
  }
});

function getResult(nums, bag) {
  const n &#61; nums.length;

  const dp &#61; new Array(bag &#43; 1).fill(0);
  dp[0] &#61; 1;

  for (let i &#61; 1; i &lt;&#61; n; i&#43;&#43;) {
    const num &#61; nums[i - 1];
    for (let j &#61; bag; j &gt;&#61; num; j--) {
      dp[j] &#61; dp[j] &#43; dp[j - num];
    }
  }

  return dp[bag];
}
</code></pre> 
<p></p> 
<h4 id="Python%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">Python算法源码</h4> 
<p>二维数组解法</p> 
<pre><code class="language-python"># 输入获取
nums &#61; list(map(int, input().split(&#34;,&#34;)))
bag &#61; int(input())


# 算法入口
def getResult():
    n &#61; len(nums)

    dp &#61; [[0] * (bag &#43; 1) for _ in range(n&#43;1)]
    dp[0][0] &#61; 1

    for i in range(1, n &#43; 1):
        num &#61; nums[i - 1]
        for j in range(bag &#43; 1):
            if j &lt; num:
                dp[i][j] &#61; dp[i - 1][j]
            else:
                dp[i][j] &#61; dp[i - 1][j] &#43; dp[i - 1][j - num]

    return dp[n][bag]


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<p>滚动数组优化解法</p> 
<pre><code class="language-python"># 输入获取
nums &#61; list(map(int, input().split(&#34;,&#34;)))
bag &#61; int(input())


# 算法入口
def getResult():
    n &#61; len(nums)

    dp &#61; [0] * (bag &#43; 1)
    dp[0] &#61; 1

    for i in range(1, n &#43; 1):
        num &#61; nums[i - 1]
        for j in range(bag, num-1, -1):
            dp[j] &#61; dp[j] &#43; dp[j - num]

    return dp[bag]


# 算法调用
print(getResult())
</code></pre> 
<p></p> 
<h4>C算法源码</h4> 
<p>二维数组解法</p> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_SIZE 30
#define MAX_ROWS 30 &#43; 1
#define MAX_COLS 100 &#43; 1 

int main()
{
	int nums[MAX_SIZE];
	int nums_size &#61; 0;
	while(scanf(&#34;%d&#34;, &amp;nums[nums_size&#43;&#43;])) {
		if(getchar() !&#61; &#39;,&#39;) break;
	}
	
	int bag;
	scanf(&#34;%d&#34;, &amp;bag);
	
	printf(&#34;%d\n&#34;, getResult(nums, nums_size, bag));
	
	return 0;
}

int getResult(int* nums, int nums_size, int bag)
{
	int dp[MAX_ROWS][MAX_COLS] &#61; {0};
	dp[0][0] &#61; 1;
	
	for(int i&#61;1; i&lt;&#61;nums_size; i&#43;&#43;) {
		int num &#61; nums[i-1];
		
		for(int j&#61;0; j&lt;&#61;bag; j&#43;&#43;) {
			if(j &lt; num) {
				dp[i][j] &#61; dp[i-1][j];
			} else {
				dp[i][j] &#61; dp[i-1][j] &#43; dp[i-1][j - num];
			}
		}
	}
	
	return dp[nums_size][bag];
}</code></pre> 
<p>滚动数组优化解法</p> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;

#define MAX_ROWS 31
#define MAX_COLS 101 

int main()
{
	int nums[MAX_ROWS];
	int nums_size &#61; 0;
	while(scanf(&#34;%d&#34;, &amp;nums[nums_size&#43;&#43;])) {
		if(getchar() !&#61; &#39;,&#39;) break;
	}
	
	int bag;
	scanf(&#34;%d&#34;, &amp;bag);
	
	printf(&#34;%d\n&#34;, getResult(nums, nums_size, bag));
	
	return 0;
}

int getResult(int* nums, int nums_size, int bag)
{
	int dp[MAX_COLS] &#61; {0};
	dp[0] &#61; 1;
	
	for(int i&#61;0; i&lt;nums_size; i&#43;&#43;) {
		int num &#61; nums[i];
		
		for(int j&#61;bag; j&gt;&#61;num; j--) {
			dp[j] &#61; dp[j] &#43; dp[j-num];
		}
	}
	
	return dp[bag];
}</code></pre>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>